<?php
/**此类用于获取基础字典表的数据，基础字典表主要维护系统中所需要用到的各种数据
 */
class DICTIONARY {
    var $language='ZH_CN';

    function DICTIONARY() {
        //$this->language = $dictLanguage;
    }

    function setLanguage($dictLanguage) {
        $this->language = $dictLanguage;
    }

    /**根据数据类型和语言来获取其记录集
     */
    function DictList($sDictType) {
        global $DB,$TABLE_DICTIONARY;
        $sql  = "Select DICTTYPE,DICTNAME,DICTVALUE,REMARK ";
        $sql .= "From $TABLE_DICTIONARY Where DictType='$sDictType' ";
        $sql .= "Order By DictName";

        return $DB->query($sql);
    }
    
    /**根据数据类型和语言来获取其记录集，并获取记录集中的DictName和DictValue值，
     * 构造成HTML Select元素中的<option ...></option>字符串返回
     */
    function DictOptionList($sDictType,$sSelected,$isPleaselect=true) {
        global $DB;

        $aKbn = $this->getDictArray($sDictType,$isPleaselect);
        foreach ($aKbn as $code => $value) {
            $sHtml .= "\t<option value='".$code.(strcmp($code,$sSelected)==0 ? "' Selected>":"'>");
            $sHtml .= $value."</option>";
        }

        return $sHtml;
    }

    /**获取类型的数组
     */
    function getDictArray($sDictType,$isPleaselect=true) {
        global $DB;

        $aa = array();
        if ($isPleaselect) {
            $aa[""] = "请选择";
        }

        $result = $this->DictList($sDictType);
        while ($row = $DB->fetchRow($result)) {
            $aa[$row["DICTNAME"]] = $row["DICTVALUE"];
        }
        $DB->freeResult($result);

        return $aa;
    }

    /**获取类型的数组
     * 将不必要的键值去掉，不显示
     */
    function getDictArray1($sDictType,$isPleaselect=true,$aNoInclude=array()) {
        global $DB;

        $aa = array();
        if ($isPleaselect) {
            $aa[""] = "请选择";
        }

        $result = $this->DictList($sDictType);
        while ($row = $DB->fetchRow($result)) {
            if (!in_array($row["DICTNAME"],$aNoInclude)) {
                $aa[$row["DICTNAME"]] = $row["DICTVALUE"];
            }
        }
        $DB->freeResult($result);

        return $aa;
    }

    /**获取ComboBox的SimpleStore数组
     */
    function getComboStore($sDictType,$isPleaselect=true) {
        global $DB;
        $dStore = "";

        if ($isPleaselect) {
            $dStore = "['','请选择']";
        }

        $result = $this->DictList($sDictType);
        $sTemp  = "";
        while ($row = $DB->fetchRow($result)) {
            $sTemp .= ",['".$row["DICTNAME"]."','".$row["DICTVALUE"]."']";
        }
        $DB->freeResult($result);

        if ($isPleaselect) {
            $dStore = "[".$dStore.$sTemp."]";
        } else {
            if ($sTemp == "") {
                $dStore = "[]";
            } else {
                $dStore = "[".substr($sTemp,1)."]";
            }
        }
        return $dStore;
    }

        /**获取ComboBox的SimpleStore数组
     */
    function getComboStore1($sDictType,$isPleaselect=true,$aNoInclude=array()) {
        global $DB;
        $dStore = "";

        if ($isPleaselect) {
            $dStore = "['','请选择']";
        }

        $result = $this->DictList($sDictType);
        $sTemp  = "";
        while ($row = $DB->fetchRow($result)) {
            if (!in_array($row["DICTNAME"],$aNoInclude)) {
                $sTemp .= ",['".$row["DICTNAME"]."','".$row["DICTVALUE"]."']";
            }
        }
        $DB->freeResult($result);

        if ($isPleaselect) {
            $dStore = "[".$dStore.$sTemp."]";
        } else {
            if ($sTemp == "") {
                $dStore = "[]";
            } else {
                $dStore = "[".substr($sTemp,1)."]";
            }
        }
        return $dStore;
    }
    
    /**根据数据类型、语言和数据名称来获取具体的值
     */
    function getDictValue($sDictType,$sDictName) {
        global $DB,$TABLE_DICTIONARY;

        $sql = "Select DICTVALUE From $TABLE_DICTIONARY Where DictType='$sDictType' and DictName='$sDictName'";
        return $DB->getFirstField($sql);
    }
}
